package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.util.BaseAction;
import cn.itcast.crm.domain.Customer;

/**
 * 定区的Action类
 * 
 * @author 王文杰
 * @version 1.0, 2017-9-5 15:39:42
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea>{

	/** 注入业务层对象 */
	@Autowired
	private FixedAreaService fixedAreaService;
	
	/**
	 * 添加定区信息的方法
	 * 
	 * @return 成功类型
	 */
	@Action(value="fixedArea_save",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String save(){
		// 调用业务层的添加方法
		fixedAreaService.add(model);
		
		return SUCCESS;
	}
	
	/**
	 * 分页
	 * @return
	 */
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		
		// 创建一个封装page 和 rows 的pageable 对象
		Pageable pageable = new PageRequest(page-1, rows);
		
//		System.out.println(model.getId());
		// 构造条件查询对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建一个封装Predicate 的list集合
				List<Predicate> list = new ArrayList<Predicate>(); 
				// 查询root根对象 fixedarea
				// 查询当前对象对应的数据
				if(StringUtils.isNotBlank(model.getId())){
					// 根据定区编号查询
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					// 根据公司 模糊查询
					Predicate p2 = cb.equal(root.get("company").as(String.class), "%"+model.getCompany()+"%");
					list.add(p2);
				}
				
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		// 调用业务层分页查询
		Page<FixedArea> pageData =  fixedAreaService.findPageData(specification,pageable);
//		System.out.println(pageData.getContent());
		
		// 调用baseAction 中方法 将结果压入 值栈顶
		pushPageDtaToValueStack(pageData);
		
		return SUCCESS;
	}
	
	/**
	 * 查询所有已关联的客户的列表信息
	 * 
	 * @return   
	 */
	@Action(value="fixedArea_findNoAssociationCustomers", results={@Result(name="success",type="json")})
	public String findNoAssociationCustomers(){
		// 使用webClient调用webservice   accept:里面写的是接收数据的类型
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8181/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		
		ActionContext.getContext().getValueStack().push(collection);

		return SUCCESS;
	}
	
	/**
	 * 查询所有关联的客户的列表信息
	 * 
	 * @return 
	 */
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers", results={@Result(name="success",type="json")})
	public String findHasAssociationFixedAreaCustomers(){
		// 使用webClient调用webservice   accept:里面写的是接收数据的类型
		Collection<? extends Customer> collection = WebClient.create("http://localhost:8181/crm_management/services/customerService/asassociationfixedareacustomers/"+model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		
		ActionContext.getContext().getValueStack().push(collection);

		return SUCCESS;
	}
	
	/** 创建属性驱动 */
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	/**
	 * 关联客户的方法
	 * 
	 * @return
	 */
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCustomersToFixedArea(){
		String customerIdStr = StringUtils.join(customerIds,",");
		// 使用webClient调用webservice   accept:里面写的是接收数据的类型
				WebClient.create("http://localhost:8181/crm_management/services/customerService"
						+ "/associationcustomerstofixedarea?customerIdStr="+customerIdStr+"&fixedAreaId="+model.getId()).put(null);
						
				return SUCCESS;
	}

	/** 属性驱动 */
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	/**
	 * 关联快递员到定区的方法
	 * 
	 * @return
	 */
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		// 调用业务层 关联快递员到定区
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
}
